<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('platform:order:sendGoods')" type="primary" @click="addOrUpdateHandle()">发货</el-button>
        <el-button v-if="isAuth('platform:order:confirm')" type="danger" @click="confirm()">确认收货</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="orderSn"
        header-align="center"
        align="center"
        label="订单号">
      </el-table-column>
      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        label="会员">
      </el-table-column>
      <el-table-column
        prop="orderType"
        header-align="center"
        align="center"
        label="订单类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.orderType === null" size="small" type="success">-</el-tag>
          <el-tag v-else-if="scope.row.orderType === 1" size="small" type="success">普通订单</el-tag>
          <el-tag v-else-if="scope.row.orderType === 2" size="small" type="success">团购订单</el-tag>
          <el-tag v-else-if="scope.row.orderType === 3" size="small" type="success">砍价订单</el-tag>
          <el-tag v-else-if="scope.row.orderType === 4" size="small" type="success">立即购买</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="orderStatus"
        header-align="center"
        align="center"
        label="订单状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.orderStatus === null" size="small" type="success">-</el-tag>
          <el-tag v-else-if="scope.row.orderStatus === 0" size="small" type="success">待付款</el-tag>
          <el-tag v-else-if="scope.row.orderStatus === 101" size="small" type="success">订单已取消</el-tag>
          <el-tag v-else-if="scope.row.orderStatus === 102" size="small" type="success">订单已删除</el-tag>
          <el-tag v-else-if="scope.row.orderStatus === 201" size="small" type="success">订单已付款</el-tag>
          <el-tag v-else-if="scope.row.orderStatus === 300" size="small" type="success">订单已发货</el-tag>
          <el-tag v-else-if="scope.row.orderStatus === 301" size="small" type="success">用户确认收货</el-tag>
          <el-tag v-else-if="scope.row.orderStatus === 401" size="small" type="success">退款</el-tag>
          <el-tag v-else-if="scope.row.orderStatus === 402" size="small" type="success">完成</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="shippingStatus"
        header-align="center"
        align="center"
        label="发货状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.shippingStatus === null" size="small" type="success">-</el-tag>
          <el-tag v-else-if="scope.row.shippingStatus === 0" size="small" type="success">未发货</el-tag>
          <el-tag v-else-if="scope.row.shippingStatus === 1" size="small" type="success">已发货</el-tag>
          <el-tag v-else-if="scope.row.shippingStatus === 2" size="small" type="success">已收货</el-tag>
          <el-tag v-else-if="scope.row.shippingStatus === 4" size="small" type="success">退货</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="payStatus"
        header-align="center"
        align="center"
        label="付款状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.payStatus === null" size="small" type="success">-</el-tag>
          <el-tag v-else-if="scope.row.payStatus === 0" size="small" type="success">未付款</el-tag>
          <el-tag v-else-if="scope.row.payStatus === 1" size="small" type="success">付款中</el-tag>
          <el-tag v-else-if="scope.row.payStatus === 2" size="small" type="success">已付款</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="shippingName"
        header-align="center"
        align="center"
        label="快递公司">
      </el-table-column>
      <el-table-column
        prop="shippingNo"
        header-align="center"
        align="center"
        label="快递单号">
      </el-table-column>
      <el-table-column
        prop="actualPrice"
        header-align="center"
        align="center"
        label="实际支付金额">
      </el-table-column>
      <el-table-column
        prop="orderPrice"
        header-align="center"
        align="center"
        label="订单总价">
      </el-table-column>
      <el-table-column
        prop="goodsPrice"
        header-align="center"
        align="center"
        label="商品总价">
      </el-table-column>
      <el-table-column
        prop="addTime"
        header-align="center"
        align="center"
        label="下单时间">
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './order-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/platform/order/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        if (this.dataListSelections.length <= 0) {
          this.$message.error('请选择一条记录！')
          return
        } else if (this.dataListSelections.length > 1) {
          this.$message.error('只能选择一条记录！')
          return
        }
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(this.dataListSelections[0].id)
        })
      },
      confirm () {
        if (this.dataListSelections.length <= 0) {
          this.$message.error('请选择一条记录！')
          return
        } else if (this.dataListSelections.length > 1) {
          this.$message.error('只能选择一条记录！')
          return
        }
        var id = this.dataListSelections[0].id
        this.$confirm(`确认收货？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/platform/order/confirm'),
            method: 'post',
            data: this.$http.adornData(id, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
